package www.wen.com.common.lib.data.network

import okhttp3.MultipartBody
import retrofit2.Call
import www.wen.com.common.R
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.di.api.executeResponse
import www.wen.com.common.lib.di.api.safeApiCall
import www.wen.com.common.lib.extension.getString
import javax.inject.Inject
import javax.inject.Singleton

/**
 * @author WEN
 * @Description: 具体参数请参考ApiService的接口说明
 * @date 2020/11/25 11:13
 */
@Singleton
class ApiHelperImpl @Inject constructor(private val apiService: ApiService) : ApiHelper {

    override suspend fun requestVerifyCode(requestVerifyBody: RequestVerifyBody): Result<VerifyCodeEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestVerifyCode(requestVerifyBody)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestChannelId(channelId: Long): Result<ChannelInfo> {
        return safeApiCall(
                call = { executeResponse(apiService.requestChannelId(channelId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun patchChannel(
            channelId: Long,
            requestBodyParameter: RequestBodyParameter,
    ): Result<ChannelInfo> {
        return safeApiCall(
                call = { executeResponse(apiService.patchChannel(channelId, requestBodyParameter)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun deleteChannel(
            channelId: Long,
    ): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.deleteChannel(channelId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun deleteGroup(groupId: String): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.deleteGroup(groupId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestInviteList(serverId: String, page: Int, size: Int): Result<InvitationEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestInviteList(serverId, page, size)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestRegister(
            type: String,
            requestRegisterBody: RequestRegisterBody,
    ): Result<LoginInfo> {
        return safeApiCall(
                call = { executeResponse(apiService.requestRegister(type, requestRegisterBody)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestSmsLogin(requestRegisterBody: RequestRegisterBody): Result<LoginInfo> {
        return safeApiCall(
                call = { executeResponse(apiService.requestSmsLogin(requestRegisterBody)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestAccessToken(code: String): Result<WxEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestAccessToken(code)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestResetPassword(body: RequestBodyParameter): Result<UseInfoEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestResetPassword(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestUploadOssFile(
            partMap: Map<String, okhttp3.RequestBody>,
            vararg file: MultipartBody.Part,
    ): Result<UpOssFileEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestUploadOssFile(partMap, *file)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun searchUserByNickName(nickName: String): Result<UseInfoEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.searchUserByNickName(nickName)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestUserInfoById(id: String): Result<UseInfoEntity> {
        return safeApiCall(
            call = { executeResponse(apiService.requestUserInfoById(id)) },
            errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestLoginOrRegisterByToken(
            type: String,
            body: RequestLoginOrRegisterByToken,
    ): Result<EmptyEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestLoginOrRegisterByToken(type, body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestRegisterByToken(body: RequestLoginOrRegisterByToken): Result<LoginInfo> {
        return safeApiCall(
                call = { executeResponse(apiService.requestRegisterByToken(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestCreateServer(body: RequestBodyParameter): Result<ServerEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestCreateServer(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServerSelfList(page: Int, size: Int): Result<ServerSelfListEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestServerSelfList(page, size)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServerGroupList(serverId: String): Result<ServerGroupEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestServerGroupList(serverId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServersMemberList(
            serverId: String,
            nickName: String?,
            page: Int,
            size: Int,
    ): Result<ServiceMemberListEntity> {
        return safeApiCall(
                call = {
                    executeResponse(
                            apiService.requestServersMemberList(
                                    serverId,
                                    nickName,
                                    page,
                                    size
                            )
                    )
                },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestUsersOnlineStatus(accids: Array<String>): Result<List<UseInfoEntity>> {
        return safeApiCall(
                call = { executeResponse(apiService.requestUsersOnlineStatus(accids)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServerList(
            serverIdOrName: String?,
            recommendFlag: String?,
            page: Int?,
            size: Int?,
    ): Result<ServerListEntity> {
        return safeApiCall(
                call = {
                    executeResponse(
                            apiService.requestServerList(
                                    serverIdOrName,
                                    recommendFlag,
                                    page,
                                    size
                            )
                    )
                },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestInviteOrAdd(
            type: String,
            body: RequestBodyParameter,
    ): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestInviteOrAdd(type, body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestVerifyMobile(body: RequestBodyParameter): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestVerifyMobile(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServerDetails(serverId: String): Result<ServerInfo> {
        return safeApiCall(
                call = { executeResponse(apiService.requestServerDetails(serverId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServerPreview(serverId: String): Result<ServerSelfListEntity.Content> {
        return safeApiCall(
                call = { executeResponse(apiService.requestServerPreview(serverId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestUpdateServerInfo(
            serverId: String,
            body: RequestBodyParameter,
    ): Result<ServerUpdateInfoEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestUpdateServerInfo(serverId, body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestCreateChannel(body: RequestBodyParameter): Result<ChannelEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestCreateChannel(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServerGroup(serverId: String): Result<List<GroupingEntity>> {
        return safeApiCall(
                call = { executeResponse(apiService.requestServerGroup(serverId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServerShareUrl(body: RequestBodyParameter): Result<ShareUrlEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestServerShareUrl(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestMemberSetting(id: String, body: RequestBodyParameter): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestMemberSetting(id, body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestCreateNewGroup(body: RequestBodyParameter): Result<GroupingEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestCreateNewGroup(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestDeleteServer(serverId: String): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestDeleteServer(serverId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestChangeOwnerOrKick(type: String, body: RequestBodyParameter): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestChangeOwnerOrKick(type, body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestChangeMobile(body: RequestBodyParameter): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestChangeMobile(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestUserInfo(body: RequestBodyParameter): Result<UseInfoEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestUserInfo(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestUserInfo(): Result<UseInfoEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestUserInfo()) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestShareFind(body: RequestBodyParameter): Result<JoinServerEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestShareFind(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestMemberInvite(body: RequestBodyParameter): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestMemberInvite(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestExitServers(serverId: String): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestExitServers(serverId)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestBatch(body: List<RequestBodyParameter>): Result<List<ChannelBatchEntity>> {
        return safeApiCall(
                call = { executeResponse(apiService.requestBatch(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestGroupSort(body: List<RequestBodyParameter>): Result<List<ChannelBatchEntity>> {
        return safeApiCall(
                call = { executeResponse(apiService.requestGroupSort(body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestStartPage(size: Int, page: Int): Result<StartPageEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestStartPage(size,page)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestProtocol(map: Map<String, String>): Result<ProtocolEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.requestProtocol(map)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestServerOfficial(): Result<ServerSelfListEntity.Content> {
        return safeApiCall(
                call = { executeResponse(apiService.requestServerOfficial()) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestUpdateGroup(groupId: String, body: RequestBodyParameter): Result<ServerGroupEntity.Content> {
        return safeApiCall(
                call = { executeResponse(apiService.updateGroup(groupId,body)) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestRemove(): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestRemove()) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override suspend fun requestTokenRevoke(): Result<String> {
        return safeApiCall(
                call = { executeResponse(apiService.requestTokenRevoke()) },
                errorMessage = getString(R.string.network_anomaly)
        )
    }

    override fun addServerChannelTopMsg(body: RequestServerChannelTopMsgBodyParameter): Call<String> {
        return apiService.addServerChannelTopMsg(body)
    }

    override suspend fun getServerChannelTopMsgList(teamId: String): Result<TopMsgEntity> {
        return safeApiCall(
                call = { executeResponse(apiService.getServerChannelTopMsgList(teamId)) },
                errorMessage = "网络异常"
        )
    }

    override fun deleteServerChannelTopMsg(uuid: String): Call<String> {
        return apiService.deleteServerChannelTopMsg(uuid)
    }

}
